package com.tgpms.web.system.service.impl;

import io.netty.util.internal.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.tgpms.util.Common;
import com.tgpms.util.PageView;
import com.tgpms.web.system.dao.SmDatasourceMapper;
import com.tgpms.web.system.entity.SmDatasource;
import com.tgpms.web.system.service.SmDatasourceService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author May
 * <p>
 * 查询语句配置表的具体实现
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
public class SmDatasourceServiceImpl implements SmDatasourceService {

    @Resource
    private SmDatasourceMapper smDatasourceMapper;

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 查询所有公司相关数据源
     *
     * @return null
     */
    @Override
    public PageView queryAll(PageView pageView) {
        List<SmDatasource> smDataSources = new ArrayList<>();
        try {
            smDataSources = smDatasourceMapper.queryAll(pageView);
            pageView.setRecords(smDataSources);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageView;
    }

    /**
     * 查询单条配置表
     *
     * @param dsCode
     * @return null
     */
    @Override
    public SmDatasource findByDsCode(String dsCode) {
        return smDatasourceMapper.selectByDsCode(dsCode);
    }

    /**
     * 增加或者修改配置表
     *
     * @param smDatasource
     * @return null
     */
    @Override
    public Integer changeSmDatasource(SmDatasource smDatasource) {
        Integer status = 1;
        SmDatasource byDsCode = smDatasourceMapper.selectByDsCode(smDatasource.getDsCode());
        if (null == byDsCode) {
            try {
                status = smDatasourceMapper.insert(smDatasource);
            } catch (Exception e) {
                status = -1;
                e.printStackTrace();
            }

        } else {
            try {
                status = smDatasourceMapper.update(smDatasource);
            } catch (Exception e) {
                status = -2;
                e.printStackTrace();
            }
        }
        return status;
    }

    /**
     * 删除操作没得说
     *
     * @param dsCode
     * @return null
     */
    @Override
    public Integer delSmDatasource(String dsCode) {
        Integer status = 1;
        SmDatasource byDsCode = smDatasourceMapper.selectByDsCode(dsCode);
        if (StringUtil.isNullOrEmpty(byDsCode.getDsCode())) {
            status = -1;
        } else {
            try {
                status = smDatasourceMapper.deleteByExample(dsCode);
            } catch (Exception e) {
                status = -2;
                e.printStackTrace();
            }
        }
        return status;
    }

    /**
     * 动态执行 SQL
     *
     * @param map
     * @return
     */
    @Override
    public List<Map<String, String>> executionSql(Map<String, String> map) {
        if (map == null) {
            return null;
        }
        Map<String, String> fixSql = Common.fixSql(map);
        List<Map<String, String>> maps = new ArrayList<>();
        try {
            maps = smDatasourceMapper.executionSql(fixSql);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return maps;
    }

    @Override
    public PageView executionSqlPageView(PageView pageView) {
        Map map = Common.fixSql(pageView.getQueryMap());
        pageView.setQueryMap(map);
        List<Map<String, String>> mapList = new ArrayList<>();
        try {
            mapList = smDatasourceMapper.querySqlAndPageView(pageView);
            pageView.setRecords(mapList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageView;
    }

	@Override
	public Integer executionSqlforUpdate(Map<String, String> map) {
		if (map == null) {
            return null;
        }
        Map<String, String> fixSql = Common.fixSql(map);
        Integer i = -1;
        try {
            i = smDatasourceMapper.executionSqlforUpdate(fixSql);
        } catch (Exception e) {
            e.printStackTrace();
            return i;
        }
        return i;
	}

//    /**
//     * 处理参数
//     *
//     * @param map
//     * @return
//     */
//    private Map<String, String> fixSql(Map<String, String> map) {
//        Map<String, String> maps = new HashMap<>();
//        String sql = map.get("sql");
//        if (null == sql) {
//            return null;
//        }
//
//        for (String param : map.keySet()) {
//            String getParams = "@" + Underline2Camel.underline2Camel(param, true);
//            char[] chars = getParams.toCharArray();
//            chars[1] += 32;
//            sql = sql.replaceAll(String.valueOf(chars), map.get(param));
//        }
//        maps.put("sql", sql);
//        return maps;
//    }

}
